package game.players.goalkeeper;

import java.awt.Point;

import game.ball.SoccerBall;
import game.players.SoccerPlayer;
import game.players.states.PlayerState;

import com.uqbar.vainilla.DeltaState;
import com.uqbar.vainilla.colissions.CollisionDetector;

public abstract class GoalkeeperState extends PlayerState{
	
	private boolean hasBall = false;
	
	
	public GoalkeeperState(SoccerPlayer player) {
		super(player);
	}

	
	public abstract void update(DeltaState deltaState);
	
	public Goalkeeper getGoalkeeper(){
		return (Goalkeeper)this.getPlayer();
	}
	
	public boolean collidesAgainstBall() {
		SoccerBall ball = this.getGoalkeeper().getScene().getSoccerBall();
		return CollisionDetector.INSTANCE.collidesCircleAgainstRect(
			ball.getX(), ball.getY(), ball.getRatio(),
			this.getGoalkeeper().getX(),this.getGoalkeeper().getY(), 
			this.getGoalkeeper().getAppearance().getWidth(),
			this.getGoalkeeper().getAppearance().getHeight());
	}
	
	public void advanceToArc() {
		Point pl = this.getGoalkeeper().getProperLocation();
		double vectorI = pl.getX() - this.getPlayer().getX() + this.getPlayer().getFeetWidth();
		double vectorJ = pl.getY() - this.getPlayer().getY() - this.getPlayer().getAppearance().getHeight() + this.getPlayer().getFeetHeight();		
		double module = Math.sqrt(Math.pow(vectorI, 2) + Math.pow(vectorJ, 2));		
		double versorI = vectorI / module;
		double versorJ = vectorJ / module;		
		if(this.hasBall()){
			SoccerBall ball = this.getGoalkeeper().getScene().getSoccerBall();
			ball.moveNormally(versorI * this.getPlayer().getMovementSpeed() / 2, versorJ * this.getPlayer().getMovementSpeed() / 2);
		}else
			this.getPlayer().move(versorI * this.getPlayer().getMovementSpeed() / 2, versorJ * this.getPlayer().getMovementSpeed() / 2);
	}
	
	public boolean hasBall(){
		return this.hasBall;
	}
	
	public void setHasBall(boolean b){
		this.hasBall = b;
	}

}
